function initBlaj () {
	$('[name=name]').addClass('jVal').data('jVal',{valid:function (val) { if (val.length == 0) return 'name required'; else if (val.length < 2) return 'invalid name'; else return ''; }});
	$('[name=email]').addClass('jVal').data('jVal',{valid:function (val) { if (val.length == 0) return 'email required'; else if (val.search(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/) < 0) return 'invalid email'; else return ''; }});
	$('[name=message]').addClass('jVal').data('jVal',{valid:function (val) { if (val.length == 0) return 'message required'; else return ''; }});
	$('[name=turing]').addClass('jVal').data('jVal',{valid:function (val) { if (( parseInt(val) || 0 ) != 2) return 'prove that you are not a machine'; else return ''; }});
	if ($('.projects').length)
		$('.projects').load('/assets/projects.html');
	if ($('.posts').length)
		$('.posts').load('/posts.html');
	if ($('.comments').length)
		$('.comments').load('comments.html', null, function () { 
			if ($('.loading', this).length > 0) {
				$(this).html('<div style="padding-bottom:25px;">No comments yet</div>'); 
			} else {
				$('.msg').each(function (i) {
					var t = /@\S+/gmi, pi = 0, msghtml = $(this).html(), newhtml = [];
					while (a = t.exec(msghtml)) {
						var parseName = a[0].toLowerCase().replace(/[^a-z0-9]/gmi, ''), matchstamp = '';
						$('.usr span').each(function (i) {
							var usrName = $(this).text().toLowerCase().replace(/[^a-z0-9]/gmi, '');
							if ( usrName == parseName )
								matchstamp = $(this).parents('.com').attr('id')
						});
						if (matchstamp)
							newhtml.push(msghtml.substring(pi, (t.lastIndex - a[0].length)) + '<a href="#' + matchstamp + '">' + a[0] + '</a>');
						else
							newhtml.push(msghtml.substring(pi, t.lastIndex));
						pi = t.lastIndex;
					}
					if (pi < msghtml.length)
						newhtml.push(msghtml.substr(pi));
					$(this).html(newhtml.join(''));
				});
				jsyn();
			}
		});
	$('.posttit a').bind('mouseenter mouseleave', function () { $(this).toggleClass('posttithover'); });
	$('a[href^=http://]').attr('target', '_blank');
	if ($.fn.jVal)	
		$.fn.jVal.init();
}
$(initBlaj);
initBlaj();
function previewComment () {
	$(document.body)
		.append('<div class="mask" style="height:' + $(document).height() + 'px; width:' + $(document.body).width() + 'px;"></div>')
		.append('<div class="preview" style="top:' + ( ( window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop ) + 20 ) + 'px;"></div>')
		.find('.mask').css({opacity:0.8}).end()
		.find('.preview')
			.html( $('.templates .com').clone().wrap('<div style="padding:10px;"></div>') )
			.prepend('<div style="text-align:center; color:#888; padding:5px 10px; font-size:15px;">comment preview</div>')
			.append(
				'<span class="btn" style="float:right;" onclick="$(\'.mask,.preview,.previewborder\').remove();">' +
					'<div class="s"></div><div class="m"><div class="c">CLOSE PREVIEW</div></div><div class="s"></div>' +
					'<div style="clear:both;"></div>' +
				'</span>' +
				'<span class="btn postComment" style="padding-right:20px; float:right;">' +
					'<div class="s"></div><div class="m"><div class="c">POST COMMENT</div></div><div class="s"></div>' +
					'<div style="clear:both;"></div>' +
				'</span>')
			.find('.com img:first')
				.attr('src', 'http://www.gravatar.com/avatar/' + MD5.hex( $('.cadd [name=email]').val() || '' ) + '?s=30')
				.end()
			.find('.com .usr')
				.html( ((new Date()).getMonth() + 1) + '/' + (new Date()).getDate() + '/' + (new Date()).getFullYear() +
					' ' + (new Date()).toLocaleTimeString() + ' | ' + 
					($('.cadd [name=url]').val() ?
						'<a target="_blank" href="' + $('.cadd [name=url]').val() + '">' + ($('.cadd [name=name]').val() || '') + '</a>' :
						($('.cadd [name=name]').val() || ''))
				).end()
			.find('.com .msg')
				.html( ($('.cadd [name=message]').val() || '')
					.replace(/<((?!(pre[ ]?|\/pre|code[ ]?|\/code|a[ ]?|\/a|br[ ]?|\/br))[^<]*)>/gmi, '&lt;$1&gt;') )
				.end()
		.end()
			.append('<div class="previewborder"></div>')
			.find('.previewborder')
				.css({width:$('.preview').width(), marginLeft:(parseInt($('.preview').css('marginLeft')) || 0 ) - 10, 
					background:'#ccc', zIndex:1345, padding:'20px', opacity:.5, border:'1px solid #aaa'});
				
	jsyn();
	$('.preview .postComment').click(function () {
		$('.previewborder').css({zIndex:($('.previewborder').css('z-index') + 1), opacity:0.8}).append(
			'<object height="100" width="100" data="/assets/img/t.swf" type="application/x-shockwave-flash"><param value="/assets/img/t.swf" name="movie"/><param value="transparent" name="wmode"/></object>');
		var href = window.location.href.split('/'), postname = href[(href.length - 1)] || href[(href.length - 2)];
		$.ajax({
			type: 'post',
			url: '/blaj.cgi?addcomment',
			data: $.toJSON({
				post: postname,
				name: $('input[name=name]').val(), 
				email: $('input[name=email]').val(), 
				update: $('select[name=update]').val(), 
				url: $('input[name=url]').val(), 
				message: $('textarea[name=message]').val()
			}),
			dataType: 'text',
			cache: false,
			success: function (data) {
				if (data.indexOf('SUCCESS') >= 0)
					$('.previewborder').html('<div style="background:#eee; padding:10px;">Your comment has been submitted and is awaiting moderation<BR><BR><input type="button" value="close" onclick="$(\'.mask,.preview,.previewborder\').remove();"></div>');
				else
					$('.previewborder').html('Unexpected error submitting your comment<BR><input type="button" value="close" onclick="$(\'.mask,.preview,.previewborder\').remove();">');
			},
			error: function () {
				$('.previewborder').html('Unexpected error submitting your comment<BR><input type="button" value="close" onclick="$(\'.mask,.preview,.previewborder\').remove();">');
			}
		});
	});
	$('.preview .com .msg').html( $('.preview .com .msg').html().replace(/[\n\r]/gmi, '<br />') );
	$('.previewborder').height($('.preview').height());
	$('.preview').css({top:($('.previewComment').offset().top - $('.preview').height() - 40)});
	$('.previewborder').css({top:($('.preview').offset().top - 10)});
}
